/*
 * @(#)xin.tianhui.cloud.weblog.core.thymeleaf.processor.element
 * @(#)ChannelProcessor.java	2018-04-06
 * 
 * Copyright © 2018-2027, All rights reserved.
 * homeanter
 */
package xin.tianhui.cloud.weblog.thymeleaf.processor;

import static org.thymeleaf.util.StringUtils.isEmptyOrWhitespace;

import org.springframework.beans.factory.annotation.Autowired;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractElementTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.templatemode.TemplateMode;

import xin.tianhui.cloud.weblog.core.model.dto.PagingDTO;
import xin.tianhui.cloud.weblog.core.model.vo.PagingVO;
import xin.tianhui.cloud.weblog.manager.IChannelMng;
import xin.tianhui.cloud.weblog.model.dto.ChannelResultDTO;
import xin.tianhui.cloud.weblog.model.dto.ChannelQueryDTO;
import xin.tianhui.cloud.weblog.thymeleaf.util.ThymeleafUtils;

/**
 * 栏目处理器
 * @ClassName: ChannelProcessor
 * @author junchen homeanter@163.com
 * @date 2018-04-06 22:55:23
 * @see TODO
 */
public class ChannelProcessor extends AbstractElementTagProcessor {

	private static final String ELEMENT_NAME = "channel";
	private static final int PRECEDENCE = 100000;
	
	private static final String BID_ATTR = "bid";
	private static final String SORT_ATTR = "sort";
	private static final String SIDX_ATTR = "sidx";
	private static final String ROWS_ATTR = "rows";
	private static final String PAGE_ATTR = "page";
	
	private Integer bid = null;
	private Integer rows = null;
	private Integer page = null;
	private String sort = null;
	private String sidx = null;
	
	@Autowired
	private IChannelMng channelMng;
	
	public ChannelProcessor(String dialectPrefix) {
		super(TemplateMode.HTML, dialectPrefix, ELEMENT_NAME, true, null, false, PRECEDENCE);
	}

	@Override
	protected void doProcess(ITemplateContext context, IProcessableElementTag elementTag,IElementTagStructureHandler handler) {
		building(context,elementTag);
		if(bid != null){
			 ChannelResultDTO channelResultDTO = channelMng.getChannelByBid(bid);
			 handler.setLocalVariable("channel",channelResultDTO);
		}else if(rows != null) {
			PagingDTO paging = new PagingDTO();
			paging.setRows(rows);
			paging.setPage(page == null ? 1 : page);
			if(!isEmptyOrWhitespace(sort) && (sort.toUpperCase().equals("DESC") || sort.toUpperCase().equals("ASC")) && !isEmptyOrWhitespace(sidx)) {
				paging.setSord(sort);
				paging.setSidx(sidx);
			}
			ChannelQueryDTO channelQuery = new ChannelQueryDTO();
			PagingVO<ChannelResultDTO> pagingVO = channelMng.channelPaging(paging,channelQuery);
			handler.setLocalVariable("paging", pagingVO);
		}
		// 删除标签
		handler.removeTags();
	}
	
	private void building(ITemplateContext context, IProcessableElementTag elementTag) {
		this.bid = ThymeleafUtils.getValueAsInteger(context, elementTag, BID_ATTR);
		this.rows = ThymeleafUtils.getValueAsInteger(context,elementTag,ROWS_ATTR);
		this.page = ThymeleafUtils.getValueAsInteger(context,elementTag,PAGE_ATTR);
		this.sort = ThymeleafUtils.getValueAsStr(context,elementTag,SORT_ATTR);
		this.sidx = ThymeleafUtils.getValueAsStr(context,elementTag,SIDX_ATTR);
	}
}
